/**
 * 个人信息-收货地址编辑页面
 */

require(['jquery','../modules/pageSize','../modules/popup'], function($, pageSetSize, popup){
    //设置html根目录字体大小
    setRootFontSize(pageSetSize);	
    var _IsSaved = false;
    var _IsValid = false;
    var _rname, _rmobile, _raddress, _rstreet, _rtype, _rlng, _rlat;
    var _data = null;
    var vMsgObj = $(".validate-tips").children("p").eq(0);
    $("#save-address").on("click", function(){
        if(_IsSaved) return;
        _IsSaved = true;     
        checkDatas();
        if(!_IsValid) return;
    });
    // 验证数据
    function checkDatas(){
        var yzmsg = "";
        _rname = $("#rec-mname").val().trim();
        _rmobile = $("#rec-mobile").val().trim();
        _raddress = $("#rec-address-choose").val().trim();
        _rlng = $("#lng").val();
        _rlat = $("#lat").val();
        _rstreet = $("#rec-street").val().trim();
        _rtype = $("#rec-type").val();
        if(_rname == ""){
            vMsgObj.text('请填写收货人姓名');            
        }else if(!mbValidator.isInRangeLength(_rname, 2, 50)){
            vMsgObj.text('收货人姓名长度必须在2-50个字符之间'); 
        }else if(_rmobile == ""){
            vMsgObj.text('请填写手机');            
        }else if((yzmsg = mbValidator.isMobile(_rmobile)) != ""){
            vMsgObj.text(yzmsg);            
        }else if(_raddress == ""){
            vMsgObj.text('请选择或定位所在地区');            
        }else if(!mbValidator.isInRangeLength(_raddress, 2, 100)){
            vMsgObj.text("所在地区长度必须在2-100个字符之间");            
        }else if(_rstreet != "" && !mbValidator.isInRangeLength(_rstreet, 2, 200)){
            vMsgObj.text("详细地址长度必须在2-200个字符之间");            
        }else{
            _IsValid = true;
        }
        if(!_IsValid){
            _IsSaved = false;
            popup.popup(".validate-tips");            
        }else{
            popup.loadPopup("正在处理中...");
            _data = {
                       "addrId":$('#addrId').val(), "name" : _rname,  "mobile" : _rmobile, "area" : _raddress, "addr" : _rstreet,
                        "def_addr" : _rtype, "store_lnt" : _rlng, "store_lat" : _rlat
                    };
            $.ajax({
                type: "POST",
                url: "/Home/Address/doAddAddress"+getFixedExtraUrl(),
                data: _data,
                dataType: "json",
                success: function (rs) {
                    popup.removeLoad();
                    popup.alertPopup(rs.msg);
                    if(rs.status){
                       setTimeout(function(){window.location.href = rs.url}, 500);
                    }
                    _IsSaved = false;
                },
                error: function () {
                    popup.removeLoad();
                    popup.alertPopup('服务器异常，请稍后重试！');
                    _IsSaved = false;
                }
            });
        }
        
    }
    //地址管理
    var height=$('#container_map').height();
    $('#marker').css("top",(height-$('#marker').outerHeight())/2+'px');
    // 设置默认地址
    $('.default-set').on('click',function(){
       if($(this).hasClass('active')){
           $(this).removeClass('active');
           $("#rec-type").val(0);
       }
        else{
           $(this).addClass('active');
           $("#rec-type").val(1);
       }
    });

    // 地图定位
    $('.location-address').on('click',function(){
        $('.location-box').show();
        // 搜索取消value值
        $('.value-delete').click(function(){
            $(this).siblings().val('');
        });
        $('.back').click(function(){
            $('.location-box').hide();
        });
    });
    $('.location-result li').on('click',function(){
        $('.location-box').hide();
    });	
	if(!isLocationChosen){
		setTimeout(function(){
			if(!isLocationChosen) {
				isLocationChosen = true;
				$('#rec-address-choose').val("");
				$('#rec-street').val("");
                $('#lng').val("");
                $('#lat').val("");
				if(!isInBMFunc){
					BMapGetLocation();
				}
				_global.promote.removeLoad();
				_global.promote.alertPopup("定位超时,请手动选择！");
			}
		}, 4000);
	}	
});